package com.zt.yongxinbao.controller;

import com.zt.yongxinbao.dto.PolicyParam;
import com.zt.yongxinbao.entity.UserPolicy;
import com.zt.yongxinbao.service.UserPolicyService;
import com.zt.yongxinbao.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/user/policy")
public class UserPolicyController {
    @Autowired
    private UserPolicyService userPolicyService;
//    根据用户名查询保单
    @GetMapping(value = "/getBill/{username}")
    public CommonResult getBill(@PathVariable String username){
        List<PolicyParam> userPolicyList=userPolicyService.selectByUsername(username);
        return CommonResult.success(userPolicyList);
    }
//    根据用户名查询将过期保单
    @GetMapping(value = "/getBillSoon/{username}")
    public CommonResult getBillSoon(@PathVariable String username){
        List<PolicyParam> userPolicyList=userPolicyService.selectWillvaildByUsername(username);
        return CommonResult.success(userPolicyList);
    }
//    根据用户名查询已过期保单
    @GetMapping(value = "/getBillLater/{username}")
    public CommonResult getBillLater(@PathVariable String username){
        List<PolicyParam> userPolicyList=userPolicyService.selectUnvaildByUsername(username);
        return CommonResult.success(userPolicyList);
    }
// 根据保单id更新保单
    @PostMapping(value = "/updateBill/{id}")
    public  CommonResult updateBill(@PathVariable Integer id,@RequestParam Integer time){
        if (userPolicyService.updateById(id,time)) return CommonResult.success("您已成功续保");
        return CommonResult.failed("续保失败，请重试");
    }
//  新增保单
    @PostMapping(value = "/insertBill")
    public CommonResult insertBill(@RequestBody UserPolicy userPolicy){
        if (userPolicyService.insert(userPolicy)) return CommonResult.success("新增保单成功");
        return CommonResult.failed("新增保单失败，请重试");
    }
}
